package crmdna.client.teacher;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.teacher.Teacher.TeacherProp;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.crmdna.Client;

@Api(name = "teacher")
public class TeacherApi {
	@ApiMethod(path="create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Named("client") String client, 
			@Named("email") String email, 
			@Named("group_id") long groupId, User user) {
		
		try {			
			TeacherProp prop = Teacher.create(client, email, groupId, 
					Utils.getLoginEmail(user));
								
			return new APIResponse(Status.SUCCESS, prop);
			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Named("client") String client) {
		
		try {						
			Client.ensureValidClient(client);
			List<TeacherProp> props = Teacher.getAll(client);
				
			return new APIResponse(Status.SUCCESS, props);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="rename", httpMethod = HttpMethod.GET)
	public APIResponse update(@Named("client") String client, 
			@Named("teacher_id") long teacherId, 
			@Nullable @Named("new_email") String newEmail, 
			@Nullable @Named("new_group_id") Long newGroupId, 
			User user) {
		
		try {			
			TeacherProp prop = Teacher.update(client, teacherId, newEmail, 
					newGroupId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, prop);			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}
	
	@ApiMethod(path="delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Named("client") String client, 
			@Named("teacher_id") long teacherId, User user) {
		
		try {
			Client.ensureValidClient(client);
			Teacher.delete(client, teacherId, Utils.getLoginEmail(user));
			
			return new APIResponse(Status.SUCCESS, "Venue [" + teacherId + "] deleted");			
		} catch (Exception ex) {
			return new APIResponse(ex);
		}		
	}		
}
